package customApi

import (
	"encoding/json"
	"fmt"
	"yyLogin/pkg/jfbym/common"

	"gitee.com/kaylee595/gkaylee/kerr"
)

type Handler interface {
	GenerateReqPayload() (map[string]string, error)
	Unmarshal(resp []byte) error
}

type baseType struct {
	req map[string]string
}

func (t *baseType) GenerateReqPayload() (map[string]string, error) {
	return t.req, nil
}

type Type90007 struct {
	baseType
	Code       int     `json:"code"`
	Data       int     `json:"data"`
	Time       float64 `json:"time"`
	UniqueCode string  `json:"unique_code"`
}

func (t *Type90007) Error() string {
	return fmt.Sprintf("code: %d, data: %d, time: %f, unique_code: %s", t.Code, t.Data, t.Time, t.UniqueCode)
}

func (t *Type90007) Unmarshal(resp []byte) error {
	b := &common.RespApiBase{}
	err := json.Unmarshal(resp, b)
	if err != nil {
		return kerr.WrapTraceStack(err)
	}
	if b.Code != 10000 {
		return kerr.WrapTraceStack(b)
	}
	err = b.UnmarshalData(t)
	if err != nil {
		return kerr.WrapTraceStack(err)
	}
	if t.Code != 0 {
		return kerr.WrapTraceStack(t)
	}
	return nil
}

// New90007 定制-X度单图旋转
// image: 需要识别图片的base64字符串
func New90007(image string) *Type90007 {
	r := new(Type90007)
	r.req = map[string]string{"type": "90007", "image": image}
	return r
}

type Type90009 struct {
	baseType
	Code       int     `json:"code"`
	Data       int     `json:"data"`
	Time       float64 `json:"time"`
	UniqueCode string  `json:"unique_code"`
}

func (t *Type90009) Error() string {
	return fmt.Sprintf("code: %d, data: %d, time: %f, unique_code: %s", t.Code, t.Data, t.Time, t.UniqueCode)
}

func (t *Type90009) Unmarshal(resp []byte) error {
	b := &common.RespApiBase{}
	err := json.Unmarshal(resp, b)
	if err != nil {
		return kerr.WrapTraceStack(err)
	}
	if b.Code != 10000 {
		return kerr.WrapTraceStack(b)
	}
	err = b.UnmarshalData(t)
	if err != nil {
		return kerr.WrapTraceStack(err)
	}
	if t.Code != 0 {
		return kerr.WrapTraceStack(t)
	}
	return nil
}

// New90009 由于90007准确度低, 该类型是询问客服后给的.
func New90009(image string) *Type90009 {
	r := new(Type90009)
	r.req = map[string]string{"type": "90009", "image": image}
	return r
}
